<template>
  <div class="HomeView">
    <!-- 顶部标题 -->
    <top-title-bar>
      <template #middle>
        <div class="middle">
          <div
            class="search"
            v-if="searchRecommendList"
            @click="$router.push('/SearchView?model=1')"
          >
            <span class="iconfont icon-fangdajing"></span>
            <van-swipe
              class="my-swipe"
              :autoplay="3000"
              indicator-color="white"
              :show-indicators="false"
              :touchable="false"
              :vertical="true"
            >
              <van-swipe-item
                v-for="index in searchRecommendList.keywodList.length"
                :key="index"
              >
                <span
                  class="value"
                  :style="{ color: index == 1 ? '#e91e63' : '#fff' }"
                  >{{ searchRecommendList.keywodList[index - 1] }}</span
                ></van-swipe-item
              >
            </van-swipe>
          </div>
        </div>
      </template>
      <template #right>
        <div class="right">
          <span class="iconfont icon-xiazai" v-show="false"></span>
          <span
            class="iconfont icon-xiaoxi"
            @click="$router.push('/MsgView')"
          ></span>
        </div>
      </template>
    </top-title-bar>
    <!-- 版心 -->
    <div class="wrap" v-if="homeData">
      <!-- 轮播图 -->
      <nav v-if="navigator">
        <div class="banner">
          <div
            class="item"
            @click="
              $router.push(
                '/GameDetail?app_id=' + navigator.head_recommend.app_id
              )
            "
          >
            <div class="img">
              <img :src="navigator.head_recommend.cover_url" alt="" />
            </div>
            <div class="text">
              <h4>{{ navigator.head_recommend.app_title }}</h4>
              <p>{{ navigator.head_recommend.short_desc }}</p>
            </div>
          </div>
        </div>

        <ul class="nav">
          <li v-for="(item, i) in navigator.nav_list" :key="item.tabid">
            <a href="" target="_blank" @click.prevent="navListTapFun(i)">
              <div class="img"><img :src="item.img" alt="" /></div>
              <span>{{ item.tabname }}</span>
            </a>
          </li>
        </ul>
      </nav>
      <!-- 游戏列表区 -->
      <main class="recommend_list">
        <div v-for="(item, index) in recommend_list" :key="index">
          <!-- flag == 0 -->
          <div class="item-flag-0" v-if="item.flag == 0">
            <div
              class="left"
              @click="
                $router.push('/GameDetail?app_id=' + item.game_info.appid)
              "
            >
              <div class="img">
                <img :src="item.game_info.applogo" alt="" />
              </div>
              <div class="text">
                <h5 class="text-ellipsis-1">{{ item.game_info.apptitle }}</h5>
                <p class="text-ellipsis-1">
                  {{
                    item.game_info.apptags
                      ? item.game_info.apptags.replaceAll(",", " · ")
                      : "游戏"
                  }}
                </p>
              </div>
            </div>
            <div class="right">
              <a
                v-if="item.game_info.appid % 2 == 0"
                :href="item.game_info.dataDownUrl"
                target="_blank"
                class="download"
                @click.stop=""
                >下载</a
              >
              <a
                v-if="item.game_info.appid % 2 == 1"
                href=""
                target="_blank"
                class="play"
                @click.prevent="$toast('未安装微信\n无法打开小程序')"
                >直接玩</a
              >
            </div>
          </div>
          <!-- flag == 1 -->
          <div class="item-flag-1" v-if="item.flag == 1">
            <div class="much">
              <div class="head">
                <h5>{{ item.label_game.title }}</h5>
                <a href="" target="_blank" @click.prevent="$router.push('/GameCategory')"
                  >更多</a
                >
              </div>
              <div class="body">
                <ul>
                  <li
                    v-for="obj in item.label_game.app_list"
                    :key="obj.appId"
                    @click="$router.push('/GameDetail?app_id=' + obj.appId)"
                  >
                    <a href="" target="_blank" @click.prevent="">
                      <div class="img"><img :src="obj.appLogo" alt="" /></div>
                      <p>{{ obj.appTitle }}</p>
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </div>
          <!-- flag == 2 -->
          <div class="item-flag-2" v-if="item.flag == 2">
            <div class="beaute">
              <div class="head">
                <h5>精品专题</h5>
                <a href="" target="_blank" @click.prevent="$router.push('/GameCategory')"
                  >更多</a
                >
              </div>
              <div class="body">
                <div
                  class="img"
                  @click="$router.push('/GameDetail?app_id=' + item.subject.id)"
                >
                  <img :src="item.subject.cover" alt="" />
                </div>
              </div>
            </div>
          </div>
          <!-- flag == 3 -->
          <div class="item-flag-3" v-if="item.flag == 3">
            <div class="beaute">
              <div class="head">
                <h5>编辑推荐</h5>
                <a href="" target="_blank" @click.prevent="$router.push('/GameCategory')"
                  >更多</a
                >
              </div>
              <div class="body">
                <div
                  class="img"
                  @click="
                    $router.push('/GameDetail?app_id=' + item.boutique.id)
                  "
                >
                  <img :src="item.boutique.cover" alt="" />
                </div>
                <h5>{{ item.boutique.title }}</h5>
              </div>
            </div>
          </div>
        </div>
      </main>
    </div>
  </div>
</template>


<script>
import TopTitleBar from "../components/TopTitleBar.vue";
import { getHomeJson } from "../assets/api/home.js";
export default {
  data() {
    return {
      homeData: null,
      navigator: null,
      recommend_list: null,
      searchRecommendList: null,
    };
  },
  methods: {
    // 获取数据
    async __dataInit() {
      // 在请求时
      this.$store.commit("showLoading");

      this.homeData = await getHomeJson();
      console.log("拿到HomeView数据", this.homeData);
      await this.dataInit();

      // 请求完成后
      this.$store.commit("hideLoading");
    },

    // 数据初始化
    dataInit() {
      this.navigator = this.homeData.navigator;
      this.recommend_list = this.homeData.recommend_list;
      this.searchRecommendList = this.homeData.searchRecommendList;
    },

    // 点击事件
    navListTapFun(i) {
      console.log(i);
      this.$router.push("/GameCategory");
    },
  },
  created() {
    this.__dataInit();
  },
  activated() {
    console.log("检查是否有数据加载失败");
    if (!this.homeData) this.__dataInit();
  },
  components: {
    TopTitleBar,
  },
};
</script>


<style lang="scss" scoped>
.HomeView {
  min-height: 100%;
  background-color: #f0f0f0;
  .TopTitleBar {
    .middle {
      .search {
        height: 28px;
        border: 1px solid #ffffff;
        box-sizing: border-box;
        border-radius: 18px;
        color: #ffffff;
        display: flex;
        align-items: center;
        .my-swipe {
          height: 28px;
          line-height: 28px;
        }
        .icon-fangdajing {
          font-size: 22px;
          font-weight: bold;
          color: #fff;
          margin-left: 8px;
        }
        .value {
          font-size: 14px;
          margin-left: 8px;
        }
      }
    }
    .right {
      .icon-xiazai,
      .icon-xiaoxi {
        font-weight: bold;
        color: #fff;
        font-size: 22px;
        margin-left: 10px;
      }
    }
  }
  .wrap {
    nav {
      padding: 12px 12px 0;
      background-color: #fff;
      margin-bottom: 10px;
      .banner {
        box-shadow: 0 8px 4px 4px rgb(233, 233, 233, 0.2);
        border-radius: 8px;
        overflow: hidden;
        .item {
          position: relative;
        }
        .img {
          height: 198px;
        }
        .text {
          position: absolute;
          height: 60px;
          box-sizing: border-box;
          padding: 12px 9px;
          bottom: 0;
          left: 0;
          right: 0;
          background-image: linear-gradient(transparent, rgba(0, 0, 0, 0.2));
          h4 {
            height: 25px;
            line-height: 25px;
            color: #ffffff;
          }
          p {
            height: 20px;
            line-height: 20px;
            color: #f4f4f4;
            font-size: 12px;
          }
        }
      }
      .nav {
        margin-top: 3px;
        display: flex;
        justify-content: space-around;

        a {
          height: 85px;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;

          // &:active {
          //   box-shadow: 0 0 80px 0 inset rgba($color: #000000, $alpha: 0.2);
          //   opacity: 0.9;
          // }
        }
        li {
          .img {
            width: 28px;
            height: 28px;
          }
          span {
            display: block;
            height: 28px;
            line-height: 28px;
            text-align: center;
            width: 3em;
            font-size: 13px;
            color: #323232;
          }
        }
      }
    }
    main {
      .item-flag-0 {
        background-color: #fff;
        padding: 0 12px;
        border-bottom: 1px solid #dddddd;
        height: 75px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        &:active {
          box-shadow: 0 0 75px 0 inset rgba(0, 0, 0, 0.2);
          opacity: 0.9;
        }
        .left {
          display: flex;
          max-width: calc(100% - 61px - 10px);
          .img {
            width: 51px;
            height: 51px;
            flex-shrink: 0;
            margin-right: 10px;
            border-radius: 6px;
            overflow: hidden;
          }
          .text {
            overflow: hidden;
            h5 {
              height: 26px;
              height: 26px;
              line-height: 26px;
            }
            p {
              height: 24px;
              line-height: 24px;
              color: #969696;
              font-size: 13px;
            }
          }
        }
        .right {
          a {
            display: block;
            border-radius: 12px;
            width: 61px;
            height: 24px;
            line-height: 26px;
            text-align: center;
            font-size: 13px;
            &.download {
              border: 1px solid #20c250;
              color: #20c250;
            }
            &.play {
              border: 1px solid #3f51b5;
              color: #3f51b5;
            }
          }
        }
      }
      .item-flag-1 {
        padding: 0 12px 13px;
        background-color: #fff;
        border-bottom: 1px solid #dddddd;
        margin: 5px 0;
        .head {
          display: flex;
          justify-content: space-between;
          h5 {
            height: 48px;
            line-height: 48px;
            color: #0e0e0e;
          }
          a {
            display: block;
            height: 48px;
            line-height: 48px;
            font-size: 12px;
            color: #969696;
          }
        }
        .body {
          ul {
            display: flex;
            overflow-y: hidden;
            overflow-x: scroll;
            &::-webkit-scrollbar {
              width: 0;
              height: 0;
            }
            &::-webkit-scrollbar-thumb {
              width: 0;
              height: 0;
            }
          }
          li {
            margin-right: 12px;
            &:last-child {
              margin-right: 0;
            }
          }
          a {
            display: block;
            width: 67px;
            .img {
              width: 67px;
              height: 67px;
              border-radius: 5px;
              overflow: hidden;
            }
          }
          p {
            margin-top: 5px;
            height: 28px;
            line-height: 14px;
            font-size: 12px;
          }
        }
      }
      .item-flag-2 {
        padding: 0 12px 16px;
        background-color: #fff;
        border-bottom: 1px solid #dddddd;
        .head {
          display: flex;
          justify-content: space-between;
          h5 {
            height: 48px;
            line-height: 48px;
            color: #0e0e0e;
          }
          a {
            display: block;
            height: 48px;
            line-height: 48px;
            font-size: 12px;
            color: #969696;
          }
        }
        .body {
          .img {
            height: 198px;
            border-radius: 8px;
            overflow: hidden;
          }
        }
      }
      .item-flag-3 {
        padding: 0 12px;
        background-color: #fff;
        border-bottom: 1px solid #dddddd;
        .head {
          display: flex;
          justify-content: space-between;
          h5 {
            height: 48px;
            line-height: 48px;
            color: #0e0e0e;
          }
          a {
            display: block;
            height: 48px;
            line-height: 48px;
            font-size: 12px;
            color: #969696;
          }
        }
        .body {
          .img {
            height: 139px;
            border-radius: 8px;
            overflow: hidden;
          }
          h5 {
            height: 48px;
            line-height: 48px;
          }
        }
      }
    }
  }
}
</style>